package com.clsz.service.impl;

import com.clsz.bean.po.WatchTimes;
import com.clsz.service.WatchTimesService;
import com.clsz.utils.ESResponse;
import com.clsz.utils.ESUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WatchTimesServiceImpl implements WatchTimesService {
    @Resource
    private ESUtils esUtils;
    @Override
    public WatchTimes searchOne(String watchTimesId) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("wt_id", watchTimesId));
        searchSourceBuilder.query(boolQueryBuilder);
        ESResponse<WatchTimes> watchTimes = esUtils.search("watch_times", searchSourceBuilder, WatchTimes.class);
        return watchTimes.getResCount() == 0 ? null : watchTimes.getData().get(0);
    }
}
